package com.youhome.business.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chen365 on 2018/05/29.
 */
@RestController
@Api(tags = "图片上传")
@Slf4j
public class UploadController {
    @Autowired
    private UploadService uploadService;

    @ApiOperation(value = "上传多图", notes = "上传多图", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(dataType = "int", name = "type", value = "类型(1:upload,2:photo,3:house,4:company,5:house-type-photo)", required = true, paramType = "path")
    @PostMapping(value = "/upload/multiple/{type}")
    public Object multipleUpload(@RequestParam("myFile") MultipartFile[] myFile, @PathVariable Integer type) throws CustomException {
        return new Result(ResultCode.SUCCESS, uploadService.upload(myFile, type));
    }

    @ApiOperation(value = "上传单图", notes = "上传单图", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(dataType = "int", name = "type", value = "类型(1:upload,2:photo,3:house,4:company,5:house-type-photo)", required = true, paramType = "path")
    @PostMapping(value = "/upload/single/{type}")
    public Object singleUpload(@RequestParam("myFile") MultipartFile myFile, @PathVariable Integer type) throws CustomException {
        return new Result(ResultCode.SUCCESS, uploadService.upload(myFile, type));
    }
}
